<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="模版名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.queryType" placeholder="操作类型" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in types" :key="index" :label="item.name"
                     :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="sizeChangeHandle()">查询</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      align="left"
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        label="模版名称">
      </el-table-column>
      <el-table-column
        prop="sign"
        header-align="center"
        label="短信签名">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        :show-overflow-tooltip="true"
        label="短信模版内容">
      </el-table-column>
      <el-table-column
        prop="platType"
        header-align="center"
        width="200px"
        label="平台类型">
        <template slot-scope="scope">
          <span v-if="scope.row.platType==1">阿里云</span>
          <span v-if="scope.row.platType==2">腾讯云</span>
          <span v-if="scope.row.platType==3">七牛云</span>
        </template>
      </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="80px">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.name)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 短信日志详细 -->
    <el-dialog title="短信日志详细" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" label-width="150px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="模版名称:"
            >{{ form.name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模版类型:">
              <span v-if="form.platType==1">阿里云</span>
              <span v-if="form.platType==2">腾讯云</span>
              <span v-if="form.platType==3">七牛云</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="form.platType==1" label="AccessKeyId:">{{ form.platKey }}</el-form-item>
            <el-form-item v-else-if="form.platType==2" label="AppId:">{{ form.platKey }}</el-form-item>
            <el-form-item v-else label="AccessKey:" >{{ form.platKey }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="form.platType==1" label="AccessKeySecret:">{{ form.platSecret }}</el-form-item>
            <el-form-item v-else-if="form.platType==2" label="AppKey:">{{ form.platSecret }}</el-form-item>
            <el-form-item v-else  label="SecretKey:">{{ form.platSecret }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模版编码：">{{ form.code }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模版状态：">
              <span v-if="form.status==0">禁用</span>
              <span v-if="form.status==1">启用</span>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.sign!=''">
            <el-form-item label="短信签名：">{{form.sign}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="短信模版内容：">{{form.content}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './smsgmessagetemplate-add-or-update'
  export default {
    data () {
      return {
        form:{

        },
        types:[
          {
            id:1,
            name:"阿里云"
          },
          {
            id:2,
            name:"腾讯云"
          },
          {
            id:3,
            name:"七牛云"
          }
        ],
        open:false,
        dataForm: {
          key: '',
          queryType:''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      /** 详细按钮操作 */
      handleView(row) {
        this.open = true;
        this.form = row;
      },
      handleStatusChange(row){
        this.$confirm(`确定要 [${row.status==1? '激活':'禁用'}] 名称=[${row.name}]吗?`,
          '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //修改状态
          this.$http({
            url: this.$http.adornUrl("/smsg/smsgmessagetemplate/updateStatus"),
            method: "get",
            params: this.$http.adornParams({
              id: row.id,
              status: row.status
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              if(row.status==1) {
                this.$message({
                  message: "激活成功",
                  type: "success",
                  duration: 1500
                });
              }else {
                this.$message({
                  message: "禁用成功",
                  type: "success",
                  duration: 1500
                });
              }
              this.getDataList()

            } else {
              this.$message.error(data.msg);
            }

          });
        }).catch(error=>{
          row.status=row.status == 1?0:1
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('smsg/smsgmessagetemplate/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'typeId':this.dataForm.queryType
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id,name) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        var names = name ? [name] : this.dataListSelections.map(item => {
          return item.name
        })
     /*   this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {*/
        this.$confirm(`确定对[名称=${names.join(',')}]进行[${name ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('smsg/smsgmessagetemplate/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
